<?php
require_once ('Controller/Mobile/Common.inc.php');
require_once ('View/Result.inc.php');
#require_once ('Model/DeviceModel.inc.php');
final class Controller extends common {
	private $Result;
	private $Loader;
	private $Callback = NULL;
	public function __construct () {
		parent::__construct();
		$trace = debug_backtrace();
		if (isset ($trace[1])) {
			$this->Loader = $trace[1]['object'];
		}
	}

	public function execude () {
		header ('Content-Type: text/xml');
		$this->Result = new Result ();
		if (array_key_exists ('action', $_REQUEST)) {
			$this->getFunction ($_REQUEST['action']);
		} else {
			$this->defaultPage ();
		}
		print $this->Result->Create ($this->Callback);
	}

	public function getFunction ($action) {
		switch ($action) {
			case 'getDeviceID';
				$this->getDeviceIDPage ();
				break;
			case 'getDeviceRegisterInfo';
				$this->getDeviceRegisterInfoPage ();
				break;
			default:
				$this->defaultPage ();
				break;
		}
	}

	public function defaultPage () {
		echo "hello";
	}


	private $DeviceID = 0;
	public function getDeviceIDPage () {
		$this->Result->setModulename (sprintf ('M%sF%s', $this->Loader->module, __FUNCTION__));
//		if (array_key_exists ('IMEI', $_REQUEST) && $_REQUEST['IMEI'] != '' && array_key_exists ('PhoneNumber', $_REQUEST) && $_REQUEST['PhoneNumber'] != '' && array_key_exists ('SimSerialNumber', $_REQUEST) && $_REQUEST['SimSerialNumber'] != '' && array_key_exists ('IMSI', $_REQUEST) && $_REQUEST['IMSI'] != '' && array_key_exists ('Manufacturer', $_REQUEST) && $_REQUEST['Manufacturer'] != '' && array_key_exists ('Model', $_REQUEST) && $_REQUEST['Model'] != '' && array_key_exists ('OS', $_REQUEST) && $_REQUEST['OS'] != '' && array_key_exists ('Version', $_REQUEST) && $_REQUEST['Version'] != '') {
		if (array_key_exists ('IMEI', $_REQUEST) && $_REQUEST['IMEI'] != '' && array_key_exists ('PhoneNumber', $_REQUEST) && array_key_exists ('SimSerialNumber', $_REQUEST) && array_key_exists ('IMSI', $_REQUEST) && array_key_exists ('Manufacturer', $_REQUEST) && $_REQUEST['Manufacturer'] != '' && array_key_exists ('Model', $_REQUEST) && $_REQUEST['Model'] != '' && array_key_exists ('OS', $_REQUEST) && $_REQUEST['OS'] != '' && array_key_exists ('Version', $_REQUEST) && $_REQUEST['Version'] != '') {
/*			$DeviceModel = new DeviceModel ($this->mysql);
			$this->Result->setStatus (200);
			$this->Result->setErrorCode (0);
			if (($this->DeviceID = $DeviceModel->CheckDeviceItemExistByIMEI ($_REQUEST['IMEI'])) == NULL) {
				$this->DeviceID = $DeviceModel->AddDeviceItem ($_REQUEST['IMEI'], $_REQUEST['PhoneNumber'], $_REQUEST['SimSerialNumber'], $_REQUEST['IMSI'], $_REQUEST['Manufacturer'], $_REQUEST['Model'], $_REQUEST['OS'], $_REQUEST['Version'], $Remark = '');
			} else {
				$this->DeviceID = $this->DeviceID[$DeviceModel->idRowName];
			}*/
			$this->Result->setStatus (200);
			$this->Result->setErrorCode (0);
			if (($this->DeviceID = $this->DeviceModel->CheckDeviceItemExistByIMEI ($_REQUEST['IMEI'])) == NULL) {
				$this->DeviceID = $this->DeviceModel->AddDeviceItem ($_REQUEST['IMEI'], $_REQUEST['PhoneNumber'], $_REQUEST['SimSerialNumber'], $_REQUEST['IMSI'], $_REQUEST['Manufacturer'], $_REQUEST['Model'], $_REQUEST['OS'], $_REQUEST['Version'], $Remark = '');
			} else {
				$this->DeviceID = $this->DeviceID[$this->DeviceModel->idRowName];
			}
			$this->Callback = array ($this, 'getDeviceIDPageCallBack');
		}
	}

	public function getDeviceIDPageCallBack ($dom, $result) {
		$result->setAttribute ('DeviceID', $this->DeviceID);
	}


	private $DeviceToCircleInfo = null;
	private $CircleInfo = null;
	public function getDeviceRegisterInfoPage () {
		$this->Result->setModulename (sprintf ('M%sF%s', $this->Loader->module, __FUNCTION__));
		if (array_key_exists ('IMEI', $_REQUEST) && $_REQUEST['IMEI'] != '' && array_key_exists ('PhoneNumber', $_REQUEST) && array_key_exists ('SimSerialNumber', $_REQUEST) && array_key_exists ('IMSI', $_REQUEST) && array_key_exists ('Manufacturer', $_REQUEST) && $_REQUEST['Manufacturer'] != '' && array_key_exists ('Model', $_REQUEST) && $_REQUEST['Model'] != '' && array_key_exists ('OS', $_REQUEST) && $_REQUEST['OS'] != '' && array_key_exists ('Version', $_REQUEST) && $_REQUEST['Version'] != '') {

			if (($this->DeviceID = $this->DeviceModel->CheckDeviceItemExistByIMEI ($_REQUEST['IMEI'])) == NULL) {
				$this->DeviceID = $this->DeviceModel->AddDeviceItem ($_REQUEST['IMEI'], $_REQUEST['PhoneNumber'], $_REQUEST['SimSerialNumber'], $_REQUEST['IMSI'], $_REQUEST['Manufacturer'], $_REQUEST['Model'], $_REQUEST['OS'], $_REQUEST['Version'], $Remark = '');
			} else {
				$this->DeviceID = $this->DeviceID[$this->DeviceModel->idRowName];
			}
			if (($this->DeviceToCircleInfo = $this->DeviceToCircleModel->GetCirclesByDevice ($this->DeviceID)) == null) {
				$this->Result->setStatus (404);
			} else {
				if (($this->CircleInfo = $this->CircleModel->CheckCircleItemExist ($this->DeviceToCircleInfo[0][$this->DeviceToCircleModel->circleIDRowName])) == null) {
					$this->Result->setStatus (401);
				} else {
					$this->Result->setStatus (200);
					$this->Result->setErrorCode (0);
					$this->Callback = array ($this, 'getDeviceRegisterInfoPageCallBack');
				}
			}
		}
	}


	public function getDeviceRegisterInfoPageCallBack ($dom, $result) {
		$item = $dom->createElement ('circle');
		foreach ($this->CircleInfo as $key => $value) {
			$item->setAttribute ($key, $value);
		}
		foreach ($this->DeviceToCircleInfo[0] as $key => $value) {
			$item->setAttribute ($key, $value);
		}
		$result->appendChild ($item);
#		$result->setAttribute ('DeviceID', $this->DeviceID);
//		print_r ();
	}

/*
	public function Post () {
		$this->Result->setModulename (sprintf ('M%sF%s', $this->Loader->module, __FUNCTION__));
		if (array_key_exists ('title', $_REQUEST) && $_REQUEST['title'] != '' && array_key_exists ('description', $_REQUEST) && $_REQUEST['description'] != '') {
			$this->Result->setStatus (200);
			$this->Result->setErrorCode (0);
		} else {
			$this->Result->setErrorCode (1);
		}
	}*/
}
?>
